<template>
  <el-form
    ref="ruleForm"
    :model="deptsForm"
    :rules="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="deptsForm.name" placeholder="" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="deptsForm.code" placeholder="" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="deptsForm.manager" placeholder="请选择">
        <el-option
          v-for="ele in list"
          :key="ele.id"
          :label="ele.username"
          :value="ele.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="deptsForm.introduce" type="textarea" placeholder="" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button type="info">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {
  addDepartmentAPI,
  reqDepartmentAPI,
  editDepartmentAPI
} from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    pid: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    validList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validName = (rule, value, callback) => {
      // 添加子级时的校验数组
      let validNameList = this.validList.filter(ele => ele.pid === this.pid).map(item => item.name)
      // const obj = validNameList.find(ele => ele.name === value)
      // if (this.isEdit) validNameList = this.validList.filter(ele => ele.id !== this.pid && ).map(item => item.name)
      // 编辑状态,同级部门名字-自己
      if (this.isEdit) {
        validNameList = this.validList.filter(ele => ele.pid === this.deptsForm.pid && ele.name !== this.deptsForm.name).map(item => item.name)
      }
      console.log(validNameList)
      if (validNameList.includes(value)) {
        callback(new Error(`部门名称：${value}被占用`))
      } else {
        callback()
      }
    }
    const validCode = (rule, value, callback) => {
      let validCodeList = this.validList.map(ele => ele.code)
      // 编辑状态
      if (this.isEdit) {
        validCodeList = this.validList.filter(ele => ele.code !== value).map(item => item.code)
      }
      if (validCodeList.includes(value)) {
        callback(new Error(`部门编码：${value}被占用`))
      } else {
        callback()
      }
      console.log(validCodeList)
    }
    // 9e27ab0
    return {
      deptsForm: {
        name: '',
        code: '',
        introduce: '',
        manager: ''
      },
      ruleForm: {
        name: [
          { required: true, message: '部门名字必填', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名字为1-50位字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码必填', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码为1-50位字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人必填', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门描述', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门编码为1-300位字符',
            trigger: 'blur'
          }
        ]
      },
      // 储存员工简单列表
      list: []
    }
  },
  created() {
    this.initSimpleList()
    if (this.isEdit) this.reqDepartment()
  },
  methods: {
    // 修改部门详情
    async doEdit() {
      try {
        const res = await editDepartmentAPI(this.deptsForm)
        console.log('', res)
        // 修改成功 关闭弹层 向父组件抛出事件
        this.$emit('close')
        // 保存数据
      } catch (err) {
        console.log('', err)
      }
    },
    // 编辑状态，数据回填
    async reqDepartment() {
      try {
        const res = await reqDepartmentAPI(this.pid)
        console.log('', res)
        // 保存数据
        this.deptsForm = res.data
      } catch (err) {
        console.log('', err)
      }
    },
    // 清空表单
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    // 添加部门
    async doAdd() {
      try {
        const obj = { ...this.deptsForm, pid: this.pid }
        const res = await addDepartmentAPI(obj)
        // 添加成功 关闭弹层 向父组件抛出事件
        this.$emit('close')
        // 更新数据
        console.log('', res)
        // 保存数据
      } catch (err) {
        console.log('', err)
      }
    },
    // 提交表单
    hSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 获取员工简单列表
    async initSimpleList() {
      try {
        const res = await getEmployeeSimple()
        console.log('', res)
        // 保存数据
        this.list = res.data
      } catch (err) {
        console.log('', err)
      }
    }
  }
}
</script>
